local zhichou = fk.CreateSkill{
  name = "investiture__zhichou",
  tags = {Skill.Compulsory}
}

Fk:loadTranslationTable{
  ["investiture__zhichou"] = "掷雠",
  [":investiture__zhichou"] = "锁定技，你的回合外，你未对其造成过伤害的角色对你造成伤害后，你于额定出牌阶段后获得一个额外的出牌阶段，"
  .."此阶段你对其使用牌无距离和次数限制，且你杀死其后奖惩改为摸三张牌。",

  ["@@investiture__zhichou-phase"] = string.format('<font color="red">%s</font>', "掷雠")
}

zhichou:addEffect(fk.Damaged, {
  mute = true,
  anim_type = "masochism",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(zhichou.name) and player == target and data.from
    and not table.contains(player:getTableMark(zhichou.name), data.from.id)
    and #player.room.logic:getActualDamageEvents(1, function (e)
      return e.data.from == player and e.data.to == data.from
    end, Player.HistoryGame) == 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player.room:notifySkillInvoked(player, zhichou.name, "masochism")
    player:broadcastSkillInvoke(zhichou.name, math.random(3,4))
    player.room:addTableMark(player, zhichou.name, data.from.id)
  end,
})

---@param player Player
---@return integer[]
local function findChou(player)
  local tos = {}
  for _, pid in ipairs(player:getTableMark(zhichou.name)) do
    if Fk:currentRoom():getPlayerById(pid):isAlive() then
      table.insert(tos, pid)
    end
  end
  return tos
end

zhichou:addEffect(fk.EventPhaseEnd, {
  mute = true,
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(zhichou.name) and target == player
    and data.phase == Player.Play and data.reason == "game_rule" and #findChou(player) > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:doIndicate(player, findChou(player))
    for _, pid in ipairs(findChou(player)) do
      player:gainAnExtraPhase(Player.Play, zhichou.name..":"..tostring(pid), true)
    end
  end,
})

zhichou:addEffect(fk.EventPhaseStart, {
  mute = true,
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(zhichou.name) and target == player
    and data.phase == Player.Play and data.reason:find(zhichou.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, zhichou.name, "offensive")
    player:broadcastSkillInvoke(zhichou.name, math.random(1,2))
    local to = room:getPlayerById(tonumber(data.reason:split(":")[2]) --[[@as integer]])
    room:setPlayerMark(to, "@@investiture__zhichou-phase", player.id)
  end,
})

zhichou:addEffect("targetmod", {
  bypass_distances = function (self, player, skill, card, to)
    if player:hasSkill(zhichou.name) and to and to:getMark("@@investiture__zhichou-phase") == player.id then
      return true
    end
  end,
  bypass_times = function (self, player, skill, scope, card, to)
    if player:hasSkill(zhichou.name) and to and to:getMark("@@investiture__zhichou-phase") == player.id then
      return true
    end
  end
})

return zhichou